package demo_table.dialog;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoRadioButton;
import com.lovo.netCRM.component.LovoTxt;
import demo_table.bean.ClassBean;
import demo_table.bean.StudentBean;
import demo_table.frame.TableFrame;
import demo_table.util.DataUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdateStudentDialog extends JDialog {

    //输入框
    protected LovoTxt nameText,ageText;
    //单选
    private LovoRadioButton lovoRadioButton;
    //下拉菜单
    private LovoComboBox lovoComboBox;
    //添加按钮
    private LovoButton lovoButton, b1, b2;

    private TableFrame tableFrame;


    public UpdateStudentDialog(TableFrame tableFrame){
        this.tableFrame = tableFrame;
        this.setSize(500,500);
        this.setLayout(null);
        //this.setDefaultCloseOperation(3);

        int id = tableFrame.getSdp().getLovoTable().getKey();
        StudentBean s = null;
        final String[] b1Str = {"取消", "修改"};

        if(id == -1){
            JOptionPane.showMessageDialog(null, "请先点击需要修改的学生");
        }else{
            s = DataUtil.getStudentById(id);

            nameText = new LovoTxt("学生姓名", 50, 50, this);
            nameText.setText(s.getName());
            nameText.setEnabled(false);
            b1 =  new LovoButton("修改", 350, 50, this);

            lovoRadioButton = new LovoRadioButton("性别", new String[]{"男", "女"},50, 100, this);
            lovoComboBox = new LovoComboBox(DataUtil.getAllClasses(), "所在班级", 50, 200, this);
            lovoButton = new LovoButton("确定修改", 50, 250, this);

            ageText = new LovoTxt("年龄", 50, 150, this);
            ageText.setEnabled(false);
            ageText.setText(s.getAge()+"");

            b2 =  new LovoButton("修改", 350, 150, this);
            StudentBean finalS = s;
            lovoButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    if(nameText.getText() == null || "".equals(nameText.getText()) || ageText.getText() == null || "".equals(ageText.getText())){
                        JOptionPane.showMessageDialog(null, "修改失败， 姓名或年龄不能为空");
                        return;
                    }

                    finalS.setName(nameText.getText());
                    try {
                        finalS.setAge(Integer.parseInt(ageText.getText()));
                    }catch (Exception e1){
                        JOptionPane.showMessageDialog(null, "修改失败， 年龄必须是数字");
                     //   e1.printStackTrace();
                        return;
                    }

                    finalS.setGender(lovoRadioButton.getItem());
                    finalS.setClassBean((ClassBean) lovoComboBox.getItem());

                    DataUtil.updateStudent(id, finalS);
                    tableFrame.getSdp().getLovoTable().updateLovoTable(DataUtil.getAllStduent());
                    JOptionPane.showMessageDialog(null, "修改成功");
                    UpdateStudentDialog.this.dispose();
                }
            });

            b1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if(b1.getText().equals("修改")){
                        b1.setText(b1Str[0]);
                        nameText.setEnabled(true);
                    }else{
                        b1.setText(b1Str[1]);
                        nameText.setEnabled(false);
                    }
                }
            });

            b2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if(b2.getText().equals("修改")){
                        b2.setText(b1Str[0]);
                        ageText.setEnabled(true);
                    }else{
                        b2.setText(b1Str[1]);
                        ageText.setEnabled(false);
                    }
                }
            });

            this.setLocationRelativeTo(null);
            this.setVisible(true);
        }
    }
}
